package DT42;

import javagprssrv.Common.CommonClass;
import javagprssrv.JGS_Param;

public class DT42_DataReader
{
    public java.lang.String recvData;
    public int C_DIR;
    public int C_PRM;
    public int C_FCB;
    public int C_FCV;
    public int C_ACD;
    public int C_FuctionCode;
    public java.lang.String A_A1;
    public int A_A2;
    public int A_A3;
    public int AFN;
    public int SEQ_TpV;
    public int SEQ_FIR;
    public int SEQ_FIN;
    public int SEQ_CON;
    public int SEQ_PSEQ_RSEQ;
    public int Pn;
    public int Fn;
    public java.lang.String Data;
    public java.lang.String AUX;
    public java.lang.String Data0;
    
    public java.lang.String ReadDT42Data(final java.lang.String Data) {
        java.lang.String ret = "";
        this.ReadCommand(Data);
        ret = ret + "A_A1=" + this.A_A1 + "^" + "A_A2=" + this.A_A2 + "^" + "A_A3=" + this.A_A3 + "^";
        ret = ret + "AFN=" + this.AFN + "^" + "PN=" + this.Pn + "^" + "FN=" + this.Fn + "^";
        ret = ret + "DATA=" + this.Data + "^" + "AUX=" + this.AUX + "^";
        java.lang.System.out.println("[ReadDT42Data] " + ret);
        return ret;
    }
    
    public java.lang.String WriteCommand() {
        java.lang.String CMD = "";
        CMD = "" + CMD + this.WriteCommand_C();
        CMD = "" + CMD + this.WriteCommand_A();
        CMD = "" + CMD + (char)this.AFN;
        CMD = "" + CMD + this.WriteCommand_SEQ();
        CMD = "" + CMD + this.WriteCommand_Pn();
        CMD = "" + CMD + this.WriteCommand_Fn();
        CMD = "" + CMD + this.Data + this.AUX;
        int intLength = CMD.length();
        intLength = intLength * 4 + 1;
        final int len_0 = intLength % 256;
        final int len_2 = java.lang.Math.round((float)(intLength / 256));
        final java.lang.String strLength = "" + (char)len_0 + (char)len_2;
        int CRC = 0;
        for (int i = 0; i < CMD.length(); ++i) {
            CRC += CMD.charAt(i);
        }
        CRC %= 256;
        CMD = "h" + strLength + strLength + 'h' + CMD + (char)CRC + '\u0016';
        return CMD;
    }
    
    private java.lang.String WriteCommand_C() {
        java.lang.String ret = "";
        int c = 0;
        if (this.C_DIR == 1) {
            c += 128;
        }
        if (this.C_PRM == 1) {
            c += 64;
        }
        if (this.C_FCB == 1) {
            c += 32;
        }
        if (this.C_FCV == 1) {
            c += 16;
        }
        c += this.C_FuctionCode;
        ret = "" + (char)c;
        return ret;
    }
    
    private java.lang.String WriteCommand_A() {
        java.lang.String ret = "";
        int xzm0 = 0;
        xzm0 = java.lang.Integer.valueOf(this.A_A1.substring(0, 1));
        int xzm2 = 0;
        xzm2 = java.lang.Integer.valueOf(this.A_A1.substring(1, 2));
        int xzm3 = 0;
        xzm3 = java.lang.Integer.valueOf(this.A_A1.substring(2, 3));
        int xzm4 = 0;
        xzm4 = java.lang.Integer.valueOf(this.A_A1.substring(3, 4));
        final int xzm_Lo = xzm0 * 16 + xzm2;
        final int xzm_Hi = xzm3 * 16 + xzm4;
        java.lang.String XZM = "";
        XZM = "" + (char)xzm_Lo + (char)xzm_Hi;
        int A2_0 = 0;
        int A2_2 = 0;
        A2_0 = this.A_A2 % 256;
        A2_2 = this.A_A2 / 256;
        java.lang.String ZDH = "";
        ZDH = "" + (char)A2_0 + (char)A2_2;
        ret = "" + XZM + ZDH + (char)this.A_A3;
        return ret;
    }
    
    private java.lang.String WriteCommand_SEQ() {
        java.lang.String ret = "";
        int seq = 0;
        if (this.SEQ_TpV == 1) {
            seq += 128;
        }
        if (this.SEQ_FIR == 1) {
            seq += 64;
        }
        if (this.SEQ_FIN == 1) {
            seq += 32;
        }
        if (this.SEQ_CON == 1) {
            seq += 16;
        }
        seq += this.SEQ_PSEQ_RSEQ;
        ret = "" + (char)seq;
        return ret;
    }
    
    private java.lang.String WriteCommand_Pn() {
        java.lang.String ret = "";
        int Pn_0 = 0;
        int Pn_2 = 0;
        final int Pn_Yu = this.Pn % 8;
        final int Pn_Shang = this.Pn / 8;
        if (this.Pn > 0) {
            Pn_0 = new Double(java.lang.Math.pow(2.0, Pn_Yu - 1)).intValue();
            Pn_2 = new Double(java.lang.Math.pow(2.0, Pn_Shang)).intValue();
        }
        ret = "" + (char)Pn_0 + (char)Pn_2;
        return ret;
    }
    
    private java.lang.String WriteCommand_Fn() {
        java.lang.String ret = "";
        int Fn_0 = 0;
        int Fn_2 = 0;
        final int Fn_Yu = this.Fn % 8;
        final int Fn_Shang = this.Fn / 8;
        if (this.Fn > 0) {
            Fn_0 = new Double(java.lang.Math.pow(2.0, Fn_Yu - 1)).intValue();
            Fn_2 = Fn_Shang;
        }
        ret = "" + (char)Fn_0 + (char)Fn_2;
        return ret;
    }
    
    public int ReadCommand(final java.lang.String _recvData) {
        int ret = -1;
        this.recvData = _recvData;
        ret = this.ReadCommand();
        return ret;
    }
    
    public int ReadCommand() {
        int ret = 1;
        final int char0 = this.recvData.charAt(0);
        final int char2 = this.recvData.charAt(5);
        final int charx = this.recvData.charAt(this.recvData.length() - 1);
        if (char0 != 104) {
            ret = -1;
            return ret;
        }
        if (char2 != 104) {
            ret = -1;
            return ret;
        }
        if (charx != 22) {
            ret = -1;
            return ret;
        }
        this.ReadCommand_C();
        this.ReadCommand_A();
        this.ReadCommand_AFN();
        this.ReadCommand_SEQ();
        this.ReadCommand_Pn();
        this.ReadCommand_Fn();
        this.ReadCommand_Data();
        this.ReadCommand_AUX();
        java.lang.String LogString = "";
        LogString = "" + LogString + "C_DIR=" + this.C_DIR + ",C_PRM=" + this.C_PRM + ",C_FCB=" + this.C_FCB + ",C_FCV=" + this.C_FCV + ",C_FunctionCode=" + this.C_FuctionCode + ",        ";
        LogString = "" + LogString + "A_A1=" + this.A_A1 + ",A_A2=" + this.A_A2 + ",A_A3=" + this.A_A3 + ",        ";
        LogString = "" + LogString + "AFN=" + this.AFN + ",        ";
        LogString = "" + LogString + "SEQ_TpV=" + this.SEQ_TpV + ",SEQ_FIR=" + this.SEQ_FIR + ",SEQ_FIN=" + this.SEQ_FIN + ",SEQ_CON=" + this.SEQ_CON + ",SEQ_PSEQ_RSEQ=" + this.SEQ_PSEQ_RSEQ + ",        ";
        LogString = "" + LogString + "Pn=" + this.Pn + ",Fn=" + this.Fn + ",Data=" + this.Data + ",AUX=" + this.AUX;
        javagprssrv.JGS_Param.myLog.ShowLog(LogString, java.lang.Boolean.TRUE, java.lang.Boolean.FALSE);
        return ret;
    }
    
    private void ReadCommand_C() {
        int intC = -1;
        intC = this.recvData.charAt(6);
        if (intC >= 128) {
            this.C_DIR = 1;
            intC -= 128;
        }
        else {
            this.C_DIR = 0;
        }
        if (intC >= 64) {
            this.C_PRM = 1;
            intC -= 64;
        }
        else {
            this.C_PRM = 0;
        }
        if (intC >= 32) {
            this.C_FCB = 1;
            intC -= 32;
        }
        else {
            this.C_FCB = 0;
        }
        if (intC >= 16) {
            this.C_FCV = 1;
            intC -= 16;
        }
        else {
            this.C_FCV = 0;
        }
        this.C_FuctionCode = intC;
    }
    
    private void ReadCommand_A() {
        final java.lang.String strA = this.recvData.substring(7, 12);
        final java.lang.String strA_A1 = strA.substring(0, 2);
        final java.lang.String strA_A2 = strA.substring(2, 4);
        final java.lang.String strA_A3 = strA.substring(4, 5);
        this.A_A1 = new javagprssrv.Common.CommonClass().String2Hex(strA_A1, "");
        final int A_A2_0 = strA_A2.charAt(0);
        final int A_A2_2 = strA_A2.charAt(1);
        this.A_A2 = A_A2_0 + A_A2_2 * 256;
        this.A_A3 = strA_A3.charAt(0);
    }
    
    private void ReadCommand_AFN() {
        this.AFN = this.recvData.charAt(12);
    }
    
    private void ReadCommand_SEQ() {
        int intSEQ = this.recvData.charAt(13);
        if (intSEQ >= 128) {
            this.SEQ_TpV = 1;
            intSEQ -= 128;
        }
        else {
            this.SEQ_TpV = 0;
        }
        if (intSEQ >= 64) {
            this.SEQ_FIR = 1;
            intSEQ -= 64;
        }
        else {
            this.SEQ_FIR = 0;
        }
        if (intSEQ >= 32) {
            this.SEQ_FIN = 1;
            intSEQ -= 32;
        }
        else {
            this.SEQ_FIN = 0;
        }
        if (intSEQ >= 16) {
            this.SEQ_CON = 1;
            intSEQ -= 16;
        }
        else {
            this.SEQ_CON = 0;
        }
        this.SEQ_PSEQ_RSEQ = intSEQ;
    }
    
    private void ReadCommand_Pn() {
        final java.lang.String strPn = this.recvData.substring(14, 16);
        final int keyPn_0 = strPn.charAt(0);
        final int keyPn_2 = strPn.charAt(1);
        int _Pn_0 = 0;
        for (int i = 0; i < 8; ++i) {
            if (java.lang.Math.pow(2.0, i) == keyPn_0) {
                _Pn_0 = i + 1;
                break;
            }
        }
        int _Pn_2 = 0;
        for (int j = 0; j < 8; ++j) {
            if (java.lang.Math.pow(2.0, j) == keyPn_2) {
                _Pn_2 = j;
                break;
            }
        }
        final int _Pn = _Pn_0 + _Pn_2 * 8;
        this.Pn = _Pn;
    }
    
    private void ReadCommand_Fn() {
        final java.lang.String strFn = this.recvData.substring(16, 18);
        final int keyFn_0 = strFn.charAt(0);
        final int keyFn_2 = strFn.charAt(1);
        int _Fn = 0;
        for (int i = 0; i < 8; ++i) {
            if (java.lang.Math.pow(2.0, i) == keyFn_0) {
                _Fn = i + 1;
                break;
            }
        }
        _Fn += keyFn_2 * 8;
        this.Fn = _Fn;
    }
    
    private void ReadCommand_Data() {
        try {
            this.Data = this.recvData.substring(18, this.recvData.length() - 1 - 1);
        }
        catch (java.lang.Exception e) {
            this.Data = "";
        }
    }
    
    private void ReadCommand_AUX() {
    }
}
